//var print = require('sys').puts;
print("Start test...");

var co1;
var co2;

function test2(s) {
  print("test 2");
  print(s);
  co2 = coroutine.running();
  print("co2 status: " + coroutine.status(co2));
  var r = coroutine.yield("yield from co2");
  print("co2 resumed by: " + r);
  return "co2 returned";
}

function test(s) {
  print(s);
  co1 = coroutine.running();
  print("co1 status: " + coroutine.status(co1));
  var result = coroutine.yield("hello");
  print(result);
  var child = coroutine.create(test2);
  result = coroutine.resume(child, "resume from co1-1");
  print("co1 resume child once: " + result);
  print("co1 status: " + coroutine.status(co1));
  print("co2 status: " + coroutine.status(co2));
  result = coroutine.resume(child, "resume from co1-2");
  print("co1 resume child twice: " + result);
  print("co1 status: " + coroutine.status(co1));
  print("co2 status: " + coroutine.status(co2));
  return s;
}

var co = coroutine.create(test);
var result = coroutine.resume(co, "test it");
print(result);
print(coroutine.status(co));
result = coroutine.resume(co, "world");
print(result);
print("the end");
result = coroutine.resume(co, "dead");
result = coroutine.resume(co1, "dead");
result = coroutine.resume(co2, "dead");
co = null;
co1 = null;
co2 = null;
